package com.gjs.resource.microservice.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gjs.common.microservice.api.IResourceMicroService;
import com.gjs.common.pojo.dto.request.PageQuery;
import com.gjs.common.pojo.dto.resource.ResourceDTO;
import com.gjs.common.pojo.dto.resource.ResourceQueryDTO;
import com.gjs.common.pojo.dto.resource.TypeDTO;
import com.gjs.resource.pojo.IEducationResourceType;
import com.gjs.resource.pojo.entity.EducationResource;
import com.gjs.resource.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
@RequiredArgsConstructor
@Slf4j
public class ResourceMicroServiceImpl implements IResourceMicroService {
    private final IEducationResourceService resourceService;
    private final IEducationResourceChannelService channelService;
    private final IEducationResourceEditionService editionService;
    private final IEducationResourcePeriodService periodService;
    private final IEducationResourceStageService stageService;
    private final IEducationResourceSubjectService subjectService;

    @Override
    public IPage<ResourceDTO> listResources(ResourceQueryDTO queryDTO, PageQuery pageQuery) {
        IPage<EducationResource> resourcePage = resourceService.page(pageQuery.buildPage(), this.buildResourceWrapper(queryDTO));
        return resourcePage.convert(ResourceMicroServiceImpl::buildResourceDTO);
    }

    @Override
    public List<ResourceDTO> listResources(ResourceQueryDTO queryDTO) {
        return resourceService.list(this.buildResourceWrapper(queryDTO))
                .stream()
                .map(ResourceMicroServiceImpl::buildResourceDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<TypeDTO> listChannels() {
        return this.convertToTypeDTO(channelService.list().stream().map(obj -> (IEducationResourceType) obj).collect(Collectors.toList()));
    }

    @Override
    public List<TypeDTO> listEditions() {
        return this.convertToTypeDTO(editionService.list().stream().map(obj -> (IEducationResourceType) obj).collect(Collectors.toList()));
    }

    @Override
    public List<TypeDTO> listPeriods() {
        return this.convertToTypeDTO(periodService.list().stream().map(obj -> (IEducationResourceType) obj).collect(Collectors.toList()));
    }

    @Override
    public List<TypeDTO> listStages() {
        return this.convertToTypeDTO(stageService.list().stream().map(obj -> (IEducationResourceType) obj).collect(Collectors.toList()));
    }

    @Override
    public List<TypeDTO> listSubjects() {
        return this.convertToTypeDTO(subjectService.list().stream().map(obj -> (IEducationResourceType) obj).collect(Collectors.toList()));
    }

    /**
     * 组装资源DTO
     */
    private static ResourceDTO buildResourceDTO(EducationResource educationResource) {
        return new ResourceDTO(educationResource.getId(), educationResource.getFileTitle(), educationResource.getRealTitle(), educationResource.getCreateTime());
    }

    /**
     * 组装资源查询条件
     */
    private Wrapper<EducationResource> buildResourceWrapper(ResourceQueryDTO queryDTO) {
        LambdaQueryWrapper<EducationResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDTO.getId() != null && queryDTO.getId() > 0, EducationResource::getId, queryDTO.getId());
        wrapper.eq(queryDTO.getChannelId() != null && queryDTO.getChannelId() > 0, EducationResource::getResourceChannelId, queryDTO.getChannelId());
        wrapper.eq(queryDTO.getEditionId() != null && queryDTO.getEditionId() > 0, EducationResource::getResourceEditionId, queryDTO.getEditionId());
        wrapper.eq(queryDTO.getPeriodId() != null && queryDTO.getPeriodId() > 0, EducationResource::getResourcePeriodId, queryDTO.getPeriodId());
        wrapper.eq(queryDTO.getStageId() != null && queryDTO.getStageId() > 0, EducationResource::getResourceStageId, queryDTO.getStageId());
        wrapper.eq(queryDTO.getSubjectId() != null && queryDTO.getSubjectId() > 0, EducationResource::getResourceSubjectId, queryDTO.getSubjectId());
        wrapper.in(queryDTO.getIds() != null && !queryDTO.getIds().isEmpty(), EducationResource::getId, queryDTO.getIds());
        wrapper.like(!StrUtil.isEmpty(queryDTO.getResourceName()), EducationResource::getFileTitle, queryDTO.getResourceName());

        return wrapper;
    }

    private List<TypeDTO> convertToTypeDTO(List<IEducationResourceType> iEducationResourceTypeList) {
        List<TypeDTO> typeDTOList = new LinkedList<>();
        for (IEducationResourceType item : iEducationResourceTypeList) {
            typeDTOList.add(new TypeDTO(item.getId(), item.getName()));
        }
        return typeDTOList;
    }
}
